package com.stone.framework.gateway.handler;

import java.util.Map;

import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.web.reactive.function.server.ServerRequest;

import com.stone.framework.core.util.Beans;
import com.stone.framework.message.ResMessage;

public class ResMessageErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(request, includeStackTrace);

        StringBuilder sb = new StringBuilder();
        String desc = sb.append("'path': ").append(errorAttributes.get("path")).append(", 'error': ")
            .append(errorAttributes.get("error")).append(", 'message': ").append(errorAttributes.get("message"))
            .toString();
        ResMessage<Void> resMessage = ResMessage.fail(desc);

        Map<String, Object> attributes = Beans.beanToMap(resMessage);
        attributes.put("status", errorAttributes.get("status")); // for http header status code
        return attributes;
    }

}
